
import java.util.*;

public class cBehaviorModule {

	static Random gen = new Random();
	public static double FOLD = 0;
	public static double CALL = 2;
	public static double CHECK = 3;
	public static double ALL_IN = 4;
	public static double RAISE = 5;
	
	/**
	 * @param percentToWin: the percent chance to win based on the player's hand rank
	 */
	public static double makeDecision(int input_rank, int input_round, int input_bet) {
		 /*int input_rank = 9;
        int input_round = 4;
        int input_bet = 1;*/
		double threshold;
		double fold;
		double call;
		double raise;
		double total;
		double limit1;
		double limit2;
		double decision;
		double randomnum;
		double round_threshold = gen.nextDouble();
		double output;
		//int cont = 0;

		//while(cont<10){
		//modify the limit for the random variable according to the round level
		if (round_threshold < 0.05){
			randomnum = gen.nextDouble();
		}
		else{
			switch (input_round){
			case 1:	randomnum = (gen.nextDouble() *0.4) + 0.3;
			break;
			case 2:	randomnum = (gen.nextDouble() *0.6) + 0.2;
			break;
			case 3:	randomnum = (gen.nextDouble() *0.8) + 0.1;
			break;
			case 4:	randomnum = gen.nextDouble();
			break;
			default: randomnum = gen.nextDouble();
			break;		
			}
		}

		//Create the threshold value according to the rank of the hand
		switch (input_rank){
		case 1: threshold = (gen.nextDouble()*0.10);
		break;
		case 2: threshold = (gen.nextDouble()*0.20);
		break;
		case 3: threshold = (gen.nextDouble()*0.20) + 0.10;
		break;
		case 4: threshold = (gen.nextDouble()*0.20) + 0.20;
		break;
		case 5: threshold = (gen.nextDouble()*0.30) + 0.30;
		break;				
		case 6: threshold = (gen.nextDouble()*0.20) + 0.50;
		break;
		case 7: threshold = (gen.nextDouble()*0.20) + 0.60;
		break;
		case 8: threshold = (gen.nextDouble()*0.20) + 0.70;
		break;
		case 9: threshold = (gen.nextDouble()*0.10) + 0.90;
		break;
		default: threshold = gen.nextDouble();
		break;
		}

		//Create the decision areas
		if(threshold <= 0.2){
			fold = 0.9 - threshold;
			call = (threshold * 1/2) + 0.1;
			raise = threshold * 1/2;
		}
		else if(threshold > 0.2 && threshold <= 0.4){
			fold = 0.9 - threshold;
			call = (threshold * 1/2) + 0.1;
			raise = threshold * 1/2;
		}
		else if(threshold > 0.4 && threshold <= 0.5){
			fold = 1.7 - (threshold * 3);
			call = (threshold * 3) - 0.9;
			raise = 0.2;
		}
		else if(threshold > 0.5 && threshold <= 0.6){
			fold = 0.2;
			call = 2.1 - (threshold * 3);
			raise = (threshold * 3) - 1.3;
		}
		else if(threshold > 0.6 && threshold <= 0.8){
			fold = 0.5 - (threshold * 1/2);
			call = 0.6 - (threshold * 1/2);
			raise = threshold - 0.1;
		}
		else{
			fold = 0.5 - (threshold * 1/2);
			call = 0.6 - (threshold * 1/2);
			raise = (threshold * 2) - 1.1;
		}
		total = fold + call + raise;

		limit1 = fold;
		limit2 = fold + call;

		/*
		System.out.println("The round is: " + input_round + ", and round threshold is: " + round_threshold);
		System.out.println("The rank is: " + input_rank);
		System.out.println("prob: " + threshold + "% chance to win");
		System.out.println("odds: " + fold + "% chance to fold, " + call + "% chance to call, " + raise + "% chance to raise");
		*/

		//output values: Fold=0, Call=2, Raise=(0,1], Check=3
		//Fold
		if(randomnum <= limit1){			
			if(input_bet == 0){
				if(gen.nextDouble() < 0.5){
					decision = randomnum / fold;
					if((decision < 0.16 && input_round == 4) || (decision < 0.24 && input_round == 3) || (decision < 0.32 && input_round == 2) || (decision < 0.4 && input_round == 1)){
						//System.out.println("Fold: " + randomnum + ", decision: " + decision);
						output = 0;
					}
					else{
						//System.out.println("Check: " + randomnum + ", decision: " + decision);
						output = 3;
					}
				}
				else{
					//System.out.println("Check: " + randomnum);
					output = 3;
				}
			}
			else{
				//System.out.println("Fold: " + randomnum);
				output = 0;
			}			
		}
		//Call
		else if (randomnum > limit1 && randomnum <= limit2){			
			if(input_bet == 0){
				decision = (randomnum - limit1) / call;
				if(decision > 0.5){
					//System.out.println("Call: " + randomnum + ", decision: " + decision);
					output = 2;
				}
				else{
					//System.out.println("Check: " + randomnum + ", decision: " + decision);
					output = 3;
				}
			}
			else{
				//System.out.println("Call: " + randomnum);
				output = 2;
			}			
		}
		//Raise
		else{
			decision = (randomnum - limit2)/raise;
			//System.out.println("Raise: " + randomnum + ", use the " + decision + "% of your chips");
			output = decision;
		}
		//System.out.println("The function output is: " + output);
		//System.out.println(input_round + "\t" + output + "\t" + threshold*100);		
		//cont++;
		//}
		return output;
	}
}
